#include "Message.h"
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>


int Vacate_apply(struct member * username_src)
{
	int Message_goal_fd;
	char input_buf[1000]={0};
	char dir[100]={0};
	char date[100]={0};
	char buf[1000]={0};
	char reason[100]={0};
	char Vacate_day[100]={0};
	time_t t;
	char username_goal[100]={0};
	int send_len=0;
	int recev_len=0;
	int ret=0;


	//创建一个名字为Message的目录
	strcpy(dir,"Vacate");
	mkdir(dir,0777);

	strcpy(input_buf,"请输入您的请假天数：\n");
	send_len=strlen(input_buf)+1;	
	ret=write(username_src->fd,&send_len,4);
	ret=write(username_src->fd,input_buf,send_len);
	ret=read(username_src->fd,&recev_len,4);
	ret=read(username_src->fd,Vacate_day,recev_len);

	strcpy(input_buf,"请输入您的请假原因：\n");
	send_len=strlen(input_buf)+1;	
	ret=write(username_src->fd,&send_len,4);
	ret=write(username_src->fd,input_buf,send_len);
	ret=read(username_src->fd,&recev_len,4);
	ret=read(username_src->fd,reason,recev_len);


	//dir赋值为写目标msg文件的路径，并打开文件
	sprintf(dir,"%s%s%s%s",dir,"/",username_src->name,".vacate");
	Message_goal_fd=open(dir,O_WRONLY|O_APPEND|O_CREAT,0777);
	//获取当地时间
	time(&t);
	strcpy(date,ctime(&t));
	sprintf(input_buf,"%s%s%c%s%s%c%s%s%c%s%s","请假人：",username_src->name,'\n',"请假原因：",reason,'\n',"请假时长（天）:",Vacate_day,'\n',"请假时间：",date);
	printf("%s\n",input_buf);
	write(Message_goal_fd,input_buf,strlen(input_buf));
	close(Message_goal_fd);

	strcpy(input_buf,"请假申请成功，请按任意键继续！\n");
	send_len=strlen(input_buf)+1;	
	ret=write(username_src->fd,&send_len,4);
	ret=write(username_src->fd,input_buf,send_len);
	ret=read(username_src->fd,&recev_len,4);
	ret=read(username_src->fd,buf,recev_len);
}

int Vacate_check(struct member * username_src)
{


	//int client_read_fd=0;
	//int username_src->fd=0;
	int ret=0;
	char buf[100]={0};
	int send_len=0;
	int recev_len=0;
	char file_content[10200]={0};
	int Message_goal_fd;
	char input_buf[10000]={0};
	char dir[100]={0};
	time_t t;
	int permit=0;
	//client_read_fd=open("client_fifo_read",O_RDWR);
	//username_src->fd=open("client_fifo_write",O_RDWR);
	permit=username_src->permision;
	printf("permision:%d\n",permit);
	if(permit==0){
		printf("user\n");
		strcpy(dir,"Vacate");
		mkdir(dir,0777);
		sprintf(dir,"%s%s%s%s",dir,"/",username_src->name,".vacate");
		Message_goal_fd=open(dir,O_RDONLY|O_APPEND|O_CREAT,0777);
		ret=read(Message_goal_fd,input_buf,10000);
		if(ret>0){
			strcpy(file_content,input_buf);
			sprintf(file_content,"%s%s",file_content,"\n请输入任意字符发送并继续操作！\n");
			send_len=strlen(file_content)+1;
			ret=write(username_src->fd,&send_len,4);
			ret=write(username_src->fd,file_content,send_len);
			ret=read(username_src->fd,&recev_len,4);
			ret=read(username_src->fd,buf,recev_len);
			close(Message_goal_fd);
		}

	}else if(permit==1)
	{
		int ret=0;
		char buf[100]={0};
		int send_len=0;
		int recev_len=0;
		char file_content[10200]={0};
		int Message_goal_fd;
		char input_buf[10000]={0};
		char dir[100]={0};
		time_t t;
		int permit=0;

		printf("root\n");
		DIR * file_dir;
		struct dirent * ptr;
		strcpy(dir,"Vacate");
		mkdir(dir,0777);

		file_dir=opendir(dir);
		int i=0;
		while((ptr=readdir(file_dir))!=NULL)
		{	
			if(ptr->d_name[0]!='.')
			{
			char file[100]={0};
				//printf("d_name:%s\n",ptr->d_name);
				sprintf(file,"%s%s%s",dir,"/",ptr->d_name);
				Message_goal_fd=open(file,O_RDONLY|O_APPEND|O_CREAT,0777);
				memset(input_buf,0,10000);
				ret=read(Message_goal_fd,input_buf,10000);
				printf("%s\n",file);
				//input_buf[strlen(input_buf)+1]='\n';
				//file_content[strlen(file_content)+1]='\n';
				if(ret>0)
				{
					sprintf(file_content,"%s%s%d%s%s",file_content,"\n",i++,"\n",input_buf);
					//strcpy(file_content,input_buf);

				}
			close(Message_goal_fd);
			}

		}
		sprintf(file_content,"%s%s",file_content,"\n请输入任意字符发送并继续操作！\n");
		send_len=strlen(file_content)+1;
		ret=write(username_src->fd,&send_len,4);
		ret=write(username_src->fd,file_content,send_len);
		ret=read(username_src->fd,&recev_len,4);
		ret=read(username_src->fd,buf,recev_len);
		close(Message_goal_fd);
	}

	remove(dir);
	return 0;
}
